import 'dart:io';
import 'package:flutter/material.dart';
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';
import 'package:lcapp/model/toast/toast.dart';
import 'package:permission_handler/permission_handler.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

enum permissionType {
  storage, //存储
  camera, //摄像头
  microphone, //麦克风
  notification, //通知
  photo, //相册
  location, //定位
}
Map _permissionNames = {
  permissionType.storage: "存储",
  permissionType.camera: "摄像头",
  permissionType.microphone: "麦克风",
  permissionType.notification: "通知",
  permissionType.photo: "相册",
  permissionType.location: "定位"
};

void inspectPermission(
  BuildContext context, {
  String? content,
  List? permissionList,
  Function? permissionCallBack,
  Function? permissionErrorCallBack,
  bool isShowToast = true,
}) async {
  if(isShowToast){
    SmartDialog.show(alignment: Alignment.topCenter, builder: (BuildContext context) {
      return Container(
        margin: EdgeInsets.symmetric(vertical: ScreenUtil().statusBarHeight+20.w,horizontal: 32.w),
        padding: EdgeInsets.all(20.w),
        width: 1.sw,
        decoration: BoxDecoration(
            color: Colors.white,
            borderRadius: BorderRadius.circular(20.w)
        ),
        child: Text(content!,style: TextStyle(
            color: Colors.black,
            fontWeight: FontWeight.w500,
            fontSize: 24.sp
        ),),
      );
    },);
  }
  bool isJurisdiction = true;
  List<Permission> permission = [];
  String permissionTipsStr = "";
  if (permissionList != null && permissionList.length > 0) {
    for (permissionType item in permissionList) {
      PermissionStatus status;
      permissionTipsStr += _permissionNames[item] + '、';
      switch (item) {
        case permissionType.camera:
          status = await Permission.camera.request();
          if (status != PermissionStatus.granted) {
            isJurisdiction = false;
          }
          permission.add(Permission.camera);
          break;
        case permissionType.storage:
          status = await Permission.storage.request();

          if (status != PermissionStatus.granted && Platform.isAndroid) {
            isJurisdiction = false;
          }
          permission.add(Permission.storage);
          break;
        case permissionType.photo:
          status = await Permission.photos.request();

          if (status != PermissionStatus.granted &&
              status != PermissionStatus.limited) {
            isJurisdiction = false;
          }
          permission.add(Permission.photos);
          break;
        case permissionType.microphone:
          status = await Permission.microphone.request();

          if (status != PermissionStatus.granted) {
            isJurisdiction = false;
          }
          permission.add(Permission.microphone);
          break;
        case permissionType.notification:
          status = await Permission.notification.request();
          if (status != PermissionStatus.granted) {
            isJurisdiction = false;
          }
          permission.add(Permission.notification);
          break;
        case permissionType.location:
          status = await Permission.location.request();
          if (status != PermissionStatus.granted) {
            isJurisdiction = false;
          }
          permission.add(Permission.location);
          break;
      }
    }
  }
  SmartDialog.dismiss();
  if (isJurisdiction) {
    if (permissionCallBack != null) {
      permissionCallBack();
    }
  } else {
    if (permissionErrorCallBack != null) {
      permissionErrorCallBack();
    }
    if (isShowToast) {
      showToast("未获取${permissionTipsStr.substring(0, permissionTipsStr.length - 1)}权限,无法使用此功能！请到设置、权限管理打开对应权限~",duration: Duration(seconds: 3));
    }
  }
}



void getAlbumPermission(BuildContext context, Function videoCallback) {
  List typeList = [
    permissionType.photo,
    permissionType.camera,
  ];
  if(Platform.isAndroid){
    typeList = [permissionType.camera,permissionType.storage,];
  }
  String tipContent = "粮策需申请相机、相册、存储的权限，以便您能够修改个人头像、发布照片或视频，请允许。拒绝或取消授权不影响使用其他服务。";
  inspectPermission(
    context,
    content: tipContent,
    permissionList: typeList,
    permissionCallBack: videoCallback,
  );
}

void getCameraPermission(BuildContext context, Function videoCallback) {
  List typeList = [
    permissionType.camera,
  ];
  String tipContent = "粮策需申请相机权限，以便您能够扫码粮策二维码查看商户信息，请允许。拒绝或取消授权不影响使用其他服务。";
  inspectPermission(
    context,
    content: tipContent,
    permissionList: typeList,
    permissionCallBack: videoCallback,
  );
}

void getLocationPermission(
  BuildContext context,
  Function videoCallback, {
  bool isShowToast = true,
  Function? errorCallBack,
}) {
  List typeList = [
    permissionType.location,
  ];
  String tipContent = "粮策需申请定位的权限，以便能够为您推荐周边精彩视频内容或者现货信息，为了提供更贴近您兴趣的本地视频及您所在地区的信息服务，请允许。拒绝或取消授权不影响使用其他服务。";
  inspectPermission(
    context,
    content: tipContent,
    permissionList: typeList,
    permissionCallBack: videoCallback,
    permissionErrorCallBack: errorCallBack,
    isShowToast: isShowToast,
  );
}
